var type = null;


    $(".LT-tab .tab-ul li").click(function(){
        var n=$(this).index();
        $(".LT-tab .tab-ul li").removeClass("borb-col53B");
        $(this).addClass("borb-col53B");
        $(".LT-tab-table>div").hide();
        $(".LT-tab-table>div").eq(n).show();
    })




function showAdd(sta)
{
	type = sta;

	$(".add-modal").show();


	// alert(1);
}

function showUp(tt)
{
	id = $(tt).data('id');
    $(".xiugai-js").show();

}

function hideUp()
{
    $(".xiugai-js").hide();
}

function showDel()
{
    $(".xiugai-js").hide();
	$(".sc-fl-tip").show();
}

function hideDel()
{
	$(".sc-fl-tip").hide();
}


function sureUp()
{
	var reason = $('.upclass').val();
	// alert(reason);

	if(reason == null||reason == ""||reason == undefined || reason.length>6)
	{
		alert('请输入新的类名并长度不大于6位');
		return false;
	}

	$.post(
		'up-class.html',
		{'id':id,'name':reason,'_csrf':$('.request-csrf').val()},
		function(res)
		{
			res = eval('('+res+')');
			if(res.code == 400)
			{
				alert(res.msg);
			}else
			{
				rest = $('li[data-id="'+id+'"] a span').html();
				str = reason+'<span>'+rest+'</span>';
				$('li[data-id="'+id+'"]').each(function(i){
					$(this).children().first().html(str);
				});
				$(".xiugai-js").hide();
				$('.upclass').val('');
				$('.move-list li[data-id="'+id+'"]').each(function(i){
					$(this).html(reason);
				});
			}
		}
	)
	return false;
}

function sureDel(tt)
{
	$.post(
		'del-class.html',
		{'id':id,'_csrf':$('.request-csrf').val()},
		function(res)
		{
			res = eval('('+res+')');
			if(res.code == 400)
			{
				alert(res.msg);
			}else
			{
				if(res.type == 0)
				{
					$('.class-dir li[data-id="-1"] span').html('('+res.num+')');

					var blue = $('.class-dir li[data-id="'+id+'"] a').attr('class');

					if(blue == 'colorbule')
					{
						$('.class-dir li[data-id="0"] a').attr('class','colorbule');

						demo1();
					}


				}else
				{
					$('.class-coll li[data-id="-1"] span').html('('+res.num+')');

					var blue = $('.class-coll li[data-id="'+id+'"] a').attr('class');

					if(blue == 'colorbule')
					{
						$('.class-coll li[data-id="0"] a').attr('class','colorbule');

						demo2();
						
					}
				}


				$('li[data-id="'+id+'"]').each(function(i){
					$(this).children().first().parent().remove();

				});
				
				$('.move-list li[data-id="'+id+'"]').each(function(i){
					$(this).remove();
				});

				$('.move[data-class="'+id+'"]').each(function(i)
				{
					 $(this).children().first().html('未分类');
					 $(this).attr('data-class',-1);
				});

				$(".sc-fl-tip").hide();

				
			}
		}
		)
}

function sureAdd()
{
	var name = $('.addclass').val();

	if(name == null||name == ""||name == undefined || name.length>6)
	{
		alert('请输入类名并长度不大于6位');
		return false;
	}
	$.post(
		'add-class.html',
		{'name':name,'type':type,'_csrf':$('.request-csrf').val()},
		function(res)
		{
			var str = "";
			var str1 = "";
			var str2 = "";
			res = eval('('+res+')');

			if(res.code == 400)
			{
				alert(res.msg)
			}else
			{
				str += "<li data-id='"+res.num+"'>";
				if(type == 0)
				{
					str += "<a onclick='dired(this)'>";
				}else
				{
					str += "<a onclick='colled(this)'>";
				}
				str += name+"<span>(0)</span></a>";
				str += "<a onclick='showUp(this)' class='xiugai-a' data-id='"+res.num+"'>";
				str += "<img src='../../themes/v1.0/images/bi.png' alt=''/></a></li>";

				str1 += '<li data-id="'+res.num+'" data-type="'+type+'" onclick="moveClass(this)" >'+name+'</li>';


				$('.addclass').val('');

				if(type == 0)
				{
					$('.LT-sc-rx .move-list').each(function(i){
						$(this).append(str1);
					});
					$('.class-dir').children().last().before(str);
				}else
				{
					$('.LT-wb-rx .move-list').each(function(i){
						$(this).append(str1);
					});
					$('.class-coll').children().last().before(str);
				}

				$(".modall-back").hide();
			}
		});
}

//防止前段多次点击
var couldRun = true;
$('.sureadd').click(function(){
	if(couldRun){
		couldRun = false;
		sureAdd();
		setTimeout(function(){
             couldRun = true;
        },1000);
	};

	// alert(111);
})


function hideAdd()
{
	$('.modall-back').hide();  
}

function dired(tt)
{
	$('.class-dir .colorbule').removeClass('colorbule')
	$(tt).addClass('colorbule');

	var id = $(tt).parent().data('id');
	$.post(
		'job-list.html',
		{'type':0,'id':id,'_csrf':$('.request-csrf').val()},
		function(res)
		{
			$('.LT-sc-rx .LT-right').html(res);
		});
}


function colled(tt)
{
	$('.class-coll .colorbule').removeClass('colorbule');
	$(tt).addClass('colorbule');

	var id = $(tt).parent().data('id');

	$.post(
		'job-list.html',
		{'type':3,'id':id,'_csrf':$('.request-csrf').val()},
		function(res)
		{
			$('.LT-wb-rx .LT-right').html(res);
		});
}

function demo1()
{
	$.post(
			'job-list.html',
			{'type':0,'id':0,'_csrf':$('.request-csrf').val()},
			function(res)
			{
				$('.LT-sc-rx .LT-right').html(res);
			});
}

function demo2()
{
	$.post(
			'job-list.html',
			{'type':3,'id':0,'_csrf':$('.request-csrf').val()},
			function(res)
			{
				$('.LT-wb-rx .LT-right').html(res);
			});

}

demo1();
demo2();


function move(tt)
{
 if ($(tt).children("i").attr("class") == "fa-angle-down fa-lg") {
        $(tt).children("i").removeClass("fa-angle-down fa-lg");
        $(tt).children("i").addClass("fa-angle-up fa-lg");
    } else {
        $(tt).children("i").removeClass("fa-angle-up fa-lg");
        $(tt).children("i").addClass("fa-angle-down fa-lg");

    }
    $(tt).next().toggle();
}

/**
 * 移动分组
 * @param  {[type]} tt [description]
 * @return {[type]}    [description]
 */
function moveClass(tt)
{
	var group = $(tt).parent().data('class');
	var group_id = $(tt).data('id');
	var type = $(tt).data('type');

	var one_num = 0;
	var two_num = 0;

	var page = 0;

	var td = $(tt).parent().parent().parent().parent().children();

	if(td.length == 1)
	{
		page = -1;
	}

	var class_id = $(td).parent().parent().prev().data('id');

	var job_id = $(tt).parent().data('job');

	$.post(
		'move-class.html',
		{'group':group,'group_id':group_id,'job_id':job_id,'_csrf':$('.request-csrf').val()},
		function(res)
		{
			res = eval('('+res+')');
			if(res.code == 400)
			{
				alert(res.msg)
			}else{
				if(type == 0)
				{

					var s = $('.class-dir li=[data-id="'+group+'"] span').html();
					var num = s.replace(/[^0-9]/ig,"");
					$('.class-dir li=[data-id="'+group+'"] span').html('('+(parseInt(num)-1)+')');


					s = $('.class-dir li=[data-id="'+group_id+'"] span').html();
					num = s.replace(/[^0-9]/ig,"");
					$('.class-dir li=[data-id="'+group_id+'"] span').html('('+(parseInt(num)+1)+')');


				}else
				{
					var s = $('.class-coll li=[data-id="'+group+'"] span').html();
					var num = s.replace(/[^0-9]/ig,"");
					$('.class-coll li=[data-id="'+group+'"] span').html('('+(parseInt(num)-1)+')');


					s = $('.class-coll li=[data-id="'+group_id+'"] span').html();
					num = s.replace(/[^0-9]/ig,"");
					$('.class-coll li=[data-id="'+group_id+'"] span').html('('+(parseInt(num)+1)+')');
				}


				if(type == 0)
				{
					var href = $('.dir .active a').attr('href');
				}else
				{
					var href = $('.coll .active a').attr('href');
				}

				if(href == undefined || href == null || href == '')
				{
					href = 'job-list.html';
				}


				$.post(
					href,
					{'type':type,'id':class_id,'paged':page,'_csrf':$('.request-csrf').val()},
					function(htm)
					{
						if(type == 0)
						{
							$('.LT-sc-rx .LT-right').html(htm);
						}else
						{
							$('.LT-wb-rx .LT-right').html(htm);
						}
					});
			}
		});

}

function delColl(tt)
{

	var class_id = $(tt).parent().parent().parent().parent().prev().data('id');

	var group_id = $(tt).data('class');

	var job_id = $(tt).data('id');

	var td = $(tt).parent().parent().parent().children();

	var page = 0;

	if(td.length == 1)
	{
		page = -1;
	}
	$.post(
		'cancel-coll.html',
		{'job_id':job_id,'group_id':group_id,'_csrf':$('.request-csrf').val()},
		function(res)
		{
			res = eval('('+res+')');
			if(res.code == 400)
			{
				alert(res.msg)
			}else{


				var s = $('.class-coll li[data-id="0"]').html();
				var num = s.replace(/[^0-9]/ig,"");
				$('.class-coll li=[data-id="0"] span').html('('+(parseInt(num)-1)+')');


				s = $('.class-coll li[data-id="'+group_id+'"] span').html();
				console.log(s);
				num = s.replace(/[^0-9]/ig,"");
				$('.class-coll li=[data-id="'+group_id+'"] span').html('('+(parseInt(num)-1)+')');

				var href = $('.coll .active a').attr('href');

				if(href == undefined || href == null || href == '')
				{
					href = 'job-list.html';
				}

				$.post(
					href,
					{'type':3,'id':class_id,'paged':page,'_csrf':$('.request-csrf').val()},
					function(htm)
					{
						$('.LT-wb-rx .LT-right').html(htm);
					});
			}
		});
}
